package com.ljm.sys.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljm.common.enums.DataStatus;
import com.ljm.common.enums.ResultStatus;
import com.ljm.common.excuption.Assert;
import com.ljm.common.page.PageDto;
import com.ljm.common.util.BeanUtils;
import com.ljm.common.util.StringUtil;
import com.ljm.sys.auth.api.SysAuthRoleApi;
import com.ljm.sys.auth.dto.SysAuthRoleDto;
import com.ljm.sys.auth.entity.SysAuthRole;
import com.ljm.sys.auth.mapper.SysAuthRoleMapper;
import com.ljm.sys.auth.service.ISysAuthRolePermissionService;
import com.ljm.sys.auth.service.ISysAuthRoleService;
import com.ljm.sys.auth.service.ISysAuthUserRoleService;
import com.ljm.sys.org.dto.SysOrgUserDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 系统角色 服务实现类
 * </p>
 *
 * @author HuamingChen
 * @since 2018-12-19
 */
@Service
@Transactional
public class SysAuthRoleServiceImpl extends ServiceImpl<SysAuthRoleMapper, SysAuthRole>
        implements ISysAuthRoleService, SysAuthRoleApi  {
    /**
     * 角色接口
     */
    @Resource
    private SysAuthRoleMapper sysRoleMapper;
    /**
     * 角色权限关联接口
     */
    @Resource
    private ISysAuthRolePermissionService sysAuthRolePermissionService;
    /**
     * 用户角色关联
     */
    @Resource
    private ISysAuthUserRoleService sysAuthUserRoleService;

    @Override
    public Set<String> getUserRoles(Long userId){
        List<String> roles = sysRoleMapper.getUserRoles(userId);
        if (StringUtil.isEmpty(roles)) {
            return Collections.emptySet();
        } else {
            return new HashSet<>(roles);
        }
    }

    @Override
    public void saveData(SysOrgUserDto userDto, SysAuthRoleDto roleDto,
                         List<String> permissionCodes){
        SysAuthRole po=new SysAuthRole();
        BeanUtils.copyProperties(roleDto,po);
        if(StringUtil.isEmpty(roleDto.getId())){
            po.setCreateTime(LocalDateTime.now());
            po.setCreater(userDto.getLoginName());
            po.setStatus(DataStatus.NORMAL.getCode());
            po.setOrgId(userDto.getOrgId());
        }
        po.setEditor(userDto.getLoginName());
        po.setEditTime(LocalDateTime.now());
        //新增权限关联
        if(!StringUtil.isEmpty(permissionCodes)){
            sysAuthRolePermissionService.saveData(po.getRoleCode(),permissionCodes,userDto);
        }
        this.saveOrUpdate(po);
    }

    @Override
    public void del(Long id){
        if(StringUtil.isEmpty(id)){
            Assert.throwException(ResultStatus.FIAL.getCode(),"主键为空");
        }
        SysAuthRole po=this.getById(id);
        this.removeById(id);
        //删除关联的权限
        sysAuthRolePermissionService.delByRole(po.getRoleCode());
    }

    @Override
    public SysAuthRoleDto getDataById(Long id){
        if(StringUtil.isEmpty(id)){
            Assert.throwException(ResultStatus.FIAL.getCode(),"主键为空");
        }
        SysAuthRoleDto dto=new SysAuthRoleDto();
        SysAuthRole po=this.getById(id);
        if(!StringUtil.isEmpty(po)){
            BeanUtils.copyProperties(po,dto);
        }
        return dto;
    }

    @Override
    public PageDto<SysAuthRoleDto> getDataByPage(String roleName, String roleCode,
                                              Integer pageNo, Integer pageSize){
        PageDto<SysAuthRoleDto> pageDto=new PageDto<>(pageNo,pageSize);
        IPage<SysAuthRole> page=new Page<>(pageNo,pageSize);
        QueryWrapper<SysAuthRole> wrapper=new QueryWrapper<>();
        if(!StringUtil.isEmpty(roleName)){
            wrapper.like("role_name",roleName);
        }
        if(!StringUtil.isEmpty(roleCode)){
            wrapper.like("role_code",roleCode);
        }
        wrapper.orderByDesc("edit_time");
        page=this.page(page,wrapper);
        if(!StringUtil.isEmpty(page.getRecords())){
            pageDto.setTotal(page.getTotal());
            pageDto.setRecords(BeanUtils.copyPropertiesByList(page.getRecords(),SysAuthRoleDto.class));
        }
        return pageDto;
    }

    @Override
    public void setSysPermissions(String roleCode, List<String> permissions, SysOrgUserDto userDto){
        //新增权限关联
        if(!StringUtil.isEmpty(permissions)){
            sysAuthRolePermissionService.saveData(roleCode,permissions,userDto);
        }else{
            sysAuthRolePermissionService.delByRole(roleCode);
        }
    }

    @Override
    public void handleUserRef(String roleCode, List<Long> usesrIds, SysOrgUserDto userDto){
        //新增权限关联
        if(StringUtil.isEmpty(usesrIds)){
            Assert.throwException(ResultStatus.FIAL.getCode(),"请选择用户");
        }
        sysAuthUserRoleService.roleRefUser(roleCode,usesrIds,userDto);
    }

    @Override
    public List<SysAuthRoleDto> getAllRole(SysOrgUserDto curUser) {
        List<SysAuthRoleDto> dtos=new ArrayList<>();
        QueryWrapper<SysAuthRole> wrapper=new QueryWrapper();
        wrapper.eq("org_id",curUser.getOrgId());
        List<SysAuthRole> pos=this.list(wrapper);
        if(!StringUtil.isEmpty(pos)){
            dtos=BeanUtils.copyPropertiesByList(pos,SysAuthRoleDto.class);
        }
        return dtos;
    }
}
